package com.potevio.simulator.strategy.impl;


import com.potevio.simulator.common.CommonFlag;
import com.potevio.simulator.common.GetHexMessage;
import com.potevio.simulator.config.MsgTypeConst;
import com.potevio.simulator.heart.HeartbeatClient;
import com.potevio.simulator.task.ChargeMsgTask;
import com.potevio.simulator.util.HexUtil;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName Deal7FCommand
 * @Description:
 * @Date 2021/7/7  9:21
 * @Author zyd
 */
public class Deal7FCommand {

    private static final Logger LOGGER = LoggerFactory.getLogger(Receive05Command.class);
    private static String[] fieldsArr10 = new String[]{"chargingPile"};
    private static String[] fieldsArr6C = new String[]{"issueOrganizationSerialNumber"};
    private static String[] fieldsArr7A = new String[]{"cardNumber", "issueOrganizationSerialNumber", "transactionTime"};
    private static String[] fieldsArr3F = new String[]{"chargeStartSOC", "cumulativeChargeKwh", "cumulativeChargeAh", "cumulativeChargeTime", "startMeterDegress", "endMeterDegress"};
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static Date date = null;

    /**
     * 发送7F报文 3.4a1
     * @param channel
     */
    public void send7FMsg(Channel channel){
        // 上报结算报文7F
        Map<String, LinkedHashMap<String, String>> map2 = null;
        try {
            map2 = new GetHexMessage().getDataDomain(MsgTypeConst.END_CHARGE_SETTLEMENT_MSG_0X7F);
            String chargeType = HeartbeatClient.chargeType;
            // 处理7F报文，从其他报文中获取对应字段值
            if("0".equals(chargeType)){
                deal7FForApp(map2.get("0x7F"),Receive05Command.date,Receive05Command.chargePortTemp);
            }else if("1".equals(chargeType)){
                map2.put("0x7F", deal7FForCard(map2.get("0x7F"),CommonFlag.chargeNum));

            }
            channel.writeAndFlush(map2);
        } catch (Exception e) {
            LOGGER.error("7F报文发送异常：{}", e);
        }
    }



    /**
     * 处理7F报文 扫码
     *
     * @param map
     * @param date
     * @return
     */
    public LinkedHashMap<String, String> deal7FForApp(LinkedHashMap<String, String> map, Date date, String chargePort) throws ParseException {
        BigDecimal quantityElectricity = ChargeMsgTask.quantityElectricity;
        Set<String> stringSet = map.keySet();
        Iterator<String> iterator = stringSet.iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            String value = map.get(key);
            if (Arrays.asList(fieldsArr10).contains(key)) {
                value = MsgTypeConst.chm_Map.get("0x10").get(key);
            } else if (Arrays.asList(fieldsArr6C).contains(key)) {
                value = MsgTypeConst.chm_Map.get("0x6C_" + chargePort).get(key);
            } else if (Arrays.asList(fieldsArr7A).contains(key)) {
                value = MsgTypeConst.chm_Map.get("0x7A_" + chargePort).get(key);
            } else if (Arrays.asList(fieldsArr3F).contains(key)) {
                value = Get3FEnum.of(key).getValue();
            } else if ("endDateTime".equals(key)) {
                value = SIMPLE_DATE_FORMAT.format(date);
            } else if ("chargeGunLocalChargeType".equals(key)) {
//                for (int i = 0; i < MsgTypeConst.CHARGE_POSTS.size(); i++) {
//                    // 选择要停止的充电枪号
//                    if(MsgTypeConst.CHARGE_POSTS.get(i).equals(chargePort)){
//                        // 将该枪号从充电中得枪号集合删除
//                        MsgTypeConst.CHARGE_POSTS.remove(i);
//                        if(MsgTypeConst.CHARGE_POSTS.size() == 0){
//                            // 发送4次00状态的34报文
//                            HexUtil.send3400(CHANNEL, 4);
//                        }
//                        // 停止对应枪号的定时任务
//                        HeartbeatClient.chargeMsgTaskFutureMaps.get(chargePort).cancel(false);
//                    }
//                }
                String cpBin = HexUtil.int2Bin(Integer.valueOf(chargePort));
                //单枪模式直流
                String addCpBin = "0" + cpBin + "01";
                value = HexUtil.bin2Hex(addCpBin);
            }
            map.put(key, value);
        }
        return map;
    }


    /**
     * 处理7F报文 刷卡3.4a1
     *
     * @param map
     * @return
     */
    public LinkedHashMap<String, String> deal7FForCard(LinkedHashMap<String, String> map, String chargePort) throws ParseException {
        Set<String> stringSet = map.keySet();
        Iterator<String> iterator = stringSet.iterator();
        while(iterator.hasNext()){
            String key = iterator.next();
            String value = map.get(key);
            if (Arrays.asList(fieldsArr10).contains(key)) {
                value = MsgTypeConst.chm_Map.get("0x10").get(key);
            } else if (Arrays.asList(fieldsArr6C).contains(key)) {
                value = MsgTypeConst.chm_Map.get("0x69_"+CommonFlag.chargeNum).get(key);
            } else if (Arrays.asList(fieldsArr7A).contains(key)) {
                value = MsgTypeConst.chm_Map.get("0x70_"+CommonFlag.chargeNum).get(key);
            } else if (Arrays.asList(fieldsArr3F).contains(key)) {
                value = Get3FEnum.of(key).getValue();
            } else if ("endDateTime".equals(key)) {
                value = SIMPLE_DATE_FORMAT.format(new Date());
            } else if ("chargeGunLocalChargeType".equals(key)) {
//                for (int i = 0; i < MsgTypeConst.CHARGE_POSTS.size(); i++) {
//                    // 选择要停止的充电枪号
//                    if(MsgTypeConst.CHARGE_POSTS.get(i).equals(chargePort)){
//                        // 将该枪号从充电中得枪号集合删除
//                        MsgTypeConst.CHARGE_POSTS.remove(i);
//                        if(MsgTypeConst.CHARGE_POSTS.size() == 0){
//                            // 发送4次00状态的34报文
//                            HexUtil.send3400(CHANNEL, 4);
//                        }
//                        // 停止对应枪号的定时任务
//                        HeartbeatClient.chargeMsgTaskFutureMaps.get(chargePort).cancel(false);
//                    }
//                }
                String cpBin = HexUtil.int2Bin(Integer.valueOf(chargePort));
                //单枪模式直流
                String addCpBin = "0" + cpBin + "01";
                value = HexUtil.bin2Hex(addCpBin);
            }
            map.put(key, value);
        }
        return map;
    }



}
